home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / mailcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-30  |  10.3 KB  |  403 lines

  1. /* mailcheck.c -- The check is in the mail... */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include "posixstat.h"
  24. #include <sys/param.h>
  25. #include "shell.h"
  26.  
  27. #ifndef MAXPATHLEN
  28. #define MAXPATHLEN 1024
  29. #endif
  30.  
  31. #ifndef NOW
  32. #define NOW ((time_t)time ((time_t *)0))
  33. #endif
  34.  
  35. typedef struct {
  36.   char *name;
  37.   time_t access_time;
  38.   time_t mod_time;
  39.   long file_size;
  40. } FILEINFO;
  41.  
  42. /* The list of remembered mail files. */
  43. FILEINFO **mailfiles = (FILEINFO **)NULL;
  44.  
  45. /* Number of mail files that we have. */
  46. int mailfiles_count = 0;
  47.  
  48. /* The last known time that mail was checked. */
  49. int last_time_mail_checked = 0;
  50.  
  51. /* Returns non-zero if it is time to check mail. */
  52. time_to_check_mail ()
  53. {
  54.   char *temp = get_string_value ("MAILCHECK");
  55.   time_t now = NOW;
  56.   unsigned seconds = 0;
  57.  
  58.   if ((!temp || sscanf (temp, "%u", &seconds) == 0) ||
  59.       ((now - last_time_mail_checked < seconds)))
  60.     return (0);
  61.  
  62.   return (1);
  63. }
  64.  
  65. /* Okay, we have checked the mail.  Perhaps I should make this function
  66.    go away. */
  67. reset_mail_timer ()
  68. {
  69.   last_time_mail_checked = NOW;
  70. }
  71.  
  72. /* Locate a file in the list.  Return index of
  73.    entry, or -1 if not found. */
  74. find_mail_file (file)
  75.      char *file;
  76. {
  77.   register int index = 0;
  78.  
  79.   while (index < mailfiles_count)
  80.     if (strcmp ((mailfiles[index])->name, file) == 0) return index;
  81.     else index++;
  82.   return (-1);
  83. }
  84.  
  85. /* Add this file to the list of remembered files. */
  86. add_mail_file (file)
  87.      char *file;
  88. {
  89.   struct stat finfo;
  90.   char *full_pathname ();
  91.   char *filename = full_pathname (file);
  92.   int index = find_mail_file (file);
  93.  
  94.   if (index > -1)
  95.     {
  96.       if (stat (filename, &finfo) == 0)
  97.     {
  98.       mailfiles[index]->mod_time = finfo.st_mtime;
  99.       mailfiles[index]->access_time = finfo.st_atime;
  100.       mailfiles[index]->file_size = (long)finfo.st_size;
  101.     }
  102.       free (filename);
  103.       return;
  104.     }
  105.  
  106.   mailfiles = (FILEINFO **)
  107.     xrealloc (mailfiles,
  108.           ((++mailfiles_count) * sizeof (FILEINFO *)));
  109.  
  110.   mailfiles[mailfiles_count - 1] = (FILEINFO *)xmalloc (sizeof (FILEINFO));
  111.   mailfiles[mailfiles_count - 1]->name = filename;
  112.   if (stat (filename, &finfo) == 0)
  113.     {
  114.       mailfiles[mailfiles_count - 1]->access_time = finfo.st_atime;
  115.       mailfiles[mailfiles_count - 1]->mod_time = finfo.st_mtime;
  116.       mailfiles[mailfiles_count - 1]->file_size = finfo.st_size;
  117.     }
  118.   else
  119.     {
  120.       mailfiles[mailfiles_count - 1]->access_time
  121.     = mailfiles[mailfiles_count - 1]->mod_time = (time_t)-1;
  122.       mailfiles[mailfiles_count - 1]->file_size = (long)-1;
  123.     }
  124. }
  125.  
  126. /* Reset the existing mail files access and modification times to zero. */
  127. reset_mail_files ()
  128. {
  129.   register int i;
  130.  
  131.   for (i = 0; i < mailfiles_count; i++)
  132.     {
  133.       mailfiles[i]->access_time = mailfiles[i]->mod_time = 0;
  134.       mailfiles[i]->file_size = (long)0;
  135.     }
  136. }
  137.  
  138. /* Free the information that we have about the remembered mail files. */
  139. free_mail_files ()
  140. {
  141.   while (mailfiles_count--)
  142.     {
  143.       free (mailfiles[mailfiles_count]->name);
  144.       free (mailfiles[mailfiles_count]);
  145.     }
  146.  
  147.   if (mailfiles)
  148.     free (mailfiles);
  149.  
  150.   mailfiles_count = 0;
  151.   mailfiles = (FILEINFO **)NULL;
  152. }
  153.  
  154. /* Return the full pathname of FILE.  Easy.  Filenames that begin
  155.    with a '/' are returned as themselves.  Other filenames have
  156.    the current working directory prepended.  A new string is
  157.    returned in either case. */
  158. char *
  159. full_pathname (file)
  160.      char *file;
  161. {
  162.   char *tilde_expand (), *disposer;
  163.  
  164.   if (*file == '~')
  165.     file = tilde_expand (file);
  166.   else
  167.     file = savestring (file);
  168.  
  169.   if (absolute_pathname (file))
  170.     if (*file == '/')
  171.       return (file);
  172.  
  173.   disposer = file;
  174.  
  175.   {
  176.     char *current_dir = (char *)xmalloc (2 + MAXPATHLEN + strlen (file));
  177.     if (!getwd (current_dir))
  178.       {
  179.     report_error (current_dir);
  180.     free (current_dir);
  181.     return ((char *)NULL);
  182.       }
  183.     strcat (current_dir, "/");
  184.  
  185.     /* Turn /foo/./bar into /foo/bar. */
  186.     if (strncmp (file, "./", 2) == 0)
  187.       file += 2;
  188.  
  189.     strcat (current_dir, file);
  190.     free (disposer);
  191.     return (current_dir);
  192.   }
  193. }
  194.  
  195. /* Return non-zero if FILE's mod date has changed. */
  196. file_mod_date_changed (file)
  197.      char *file;
  198. {
  199.   time_t time = (time_t)NULL;
  200.   struct stat finfo;
  201.  
  202.   int index = find_mail_file (file);
  203.   if (index != -1)
  204.     time = mailfiles[index]->mod_time;
  205.  
  206.   if (stat (file, &finfo) == 0)
  207.     {
  208.       if (finfo.st_size != 0)
  209.     return (time != finfo.st_mtime);
  210.     }
  211.   return (0);
  212. }
  213.  
  214. /* Return non-zero if FILE's access date has changed. */
  215. file_access_date_changed (file)
  216.      char *file;
  217. {
  218.   time_t time = (time_t)NULL;
  219.   struct stat finfo;
  220.  
  221.   int index = find_mail_file (file);
  222.   if (index != -1)
  223.     time = mailfiles[index]->access_time;
  224.  
  225.   if (stat (file, &finfo) == 0)
  226.     {
  227.       if (finfo.st_size != 0)
  228.     return (time != finfo.st_atime);
  229.     }
  230.   return (0);
  231. }
  232.  
  233. /* Return non-zero if FILE's size has increased. */
  234. file_has_grown (file)
  235.      char *file;
  236. {
  237.   long size = 0L;
  238.   struct stat finfo;
  239.  
  240.   int i = find_mail_file (file);
  241.   if (i != -1)
  242.     size = mailfiles[i]->file_size;
  243.  
  244.   if (stat (file, &finfo) == 0)
  245.     {
  246.       return (finfo.st_size > size);
  247.     }
  248.   return (0);
  249. }
  250.  
  251. #if defined (USG)
  252. #define DEFAULT_MAIL_PATH "/usr/mail/"
  253. #else
  254. #define DEFAULT_MAIL_PATH "/usr/spool/mail/"
  255. #endif
  256.  
  257. /* Return the colon separated list of pathnames to check for mail. */
  258. char *
  259. get_mailpaths ()
  260. {
  261.   extern char *current_user_name;
  262.   char *mailpaths = get_string_value ("MAILPATH");
  263.   if (!mailpaths) mailpaths = get_string_value ("MAIL");
  264.   if (!mailpaths)
  265.     {
  266.       mailpaths = (char *)alloca (1 + strlen (DEFAULT_MAIL_PATH)
  267.                   + strlen (current_user_name));
  268.       strcpy (mailpaths, DEFAULT_MAIL_PATH);
  269.       strcat (mailpaths, current_user_name);
  270.     }
  271.   return (savestring (mailpaths));
  272. }
  273.  
  274. /* Remember the dates of the files specified by MAILPATH, or if there is
  275.    no MAILPATH, by the file specified in MAIL.  If neither exists, use a
  276.    default value, which we randomly concoct from using Unix. */
  277. remember_mail_dates ()
  278. {
  279.   char *mailpaths = get_mailpaths ();
  280.   char *mailfile, *extract_colon_unit ();
  281.   int index = 0;
  282.   
  283.   while (mailfile = extract_colon_unit (mailpaths, &index))
  284.     {
  285.       register int i;
  286.       for (i = 0;
  287.        mailfile[i] && mailfile[i] != '?' && mailfile[i] != '%';
  288.        i++);
  289.       mailfile[i] = '\0';
  290.       add_mail_file (mailfile);
  291.     }
  292. }
  293.  
  294. /* check_mail () is useful for more than just checking mail.  Since it has
  295.    the paranoids dream ability of telling you when someone has read your
  296.    mail, it can just as easily be used to tell you when someones .profile
  297.    file has been read, thus letting one know when someone else has logged
  298.    in.  Pretty good, huh? */
  299.  
  300. /* Check for mail in some files.  If the modification date of any
  301.    of the files in MAILPATH has changed since we last did a
  302.    remember_mail_dates () then mention that the user has mail.
  303.    Special hack:  If the shell variable MAIL_WARNING is on and the
  304.    mail file has been accessed since the last time we remembered, then
  305.    the message "The mail in <mailfile> has been read" is printed. */
  306. check_mail ()
  307. {
  308.   register int string_index;
  309.   char *extract_colon_unit ();
  310.   char *current_mail_file, *you_have_mail_message;
  311.   char *mailpaths = get_mailpaths ();
  312.   int index = 0;
  313.   char *dollar_underscore = get_string_value ("_");
  314.  
  315.   while ((current_mail_file = extract_colon_unit (mailpaths, &index)))
  316.     {
  317.       char *t;
  318.       int use_user_notification;
  319.  
  320.       if (!*current_mail_file)
  321.     {
  322.       free (current_mail_file);
  323.       continue;
  324.     }
  325.  
  326.       t = full_pathname (current_mail_file);
  327.       free (current_mail_file);
  328.       current_mail_file = t;
  329.  
  330.       use_user_notification = 0;
  331.       you_have_mail_message = "You have mail in $_";
  332.  
  333.       for (string_index = 0; current_mail_file[string_index]; string_index++)
  334.     if (current_mail_file[string_index] == '?'
  335.         || current_mail_file[string_index] == '%')
  336.       {
  337.         current_mail_file[string_index] = '\0';
  338.         you_have_mail_message = current_mail_file + string_index + 1;
  339.         use_user_notification++;
  340.         break;
  341.       }
  342.  
  343.       if (file_mod_date_changed (current_mail_file))
  344.     {
  345.       WORD_LIST *tlist, *expand_string ();
  346.       char *string_list ();
  347.       int i, file_is_bigger;
  348.       bind_variable ("_", current_mail_file);
  349.  
  350.       /* Have to compute this before the call to add_mail_file, which
  351.          resets all the information. */
  352.       file_is_bigger = file_has_grown (current_mail_file);
  353.  
  354.       add_mail_file (current_mail_file);
  355.  
  356.       i = find_mail_file (current_mail_file);
  357.  
  358.       /* If the user has just run a program which manipulates the
  359.          mail file, then don't bother explaining that the mail
  360.          file has been manipulated.  Since some systems don't change
  361.          the access time to be equal to the modification time when
  362.          the mail in the file is manipulated, check the size also.  If
  363.          the file has not grown, continue. */
  364.       if (i != -1 &&
  365.           (mailfiles[i]->access_time == mailfiles[i]->mod_time) &&
  366.           !file_is_bigger)
  367.         goto next_mail_file;
  368.  
  369.       if (!use_user_notification)
  370.         {
  371.           if (i != -1 &&
  372.           (mailfiles[i]->access_time < mailfiles[i]->mod_time) &&
  373.           file_is_bigger)
  374.         you_have_mail_message = "You have new mail in $_";
  375.         }
  376.  
  377.       if ((tlist = expand_string (you_have_mail_message, 1)))
  378.         {
  379.           char *tem = string_list (tlist);
  380.           you_have_mail_message = (char *)alloca (1 + strlen (tem));
  381.           strcpy (you_have_mail_message, tem);
  382.           free (tem);
  383.         }
  384.       else
  385.         you_have_mail_message = "";
  386.  
  387.       printf ("%s\n", you_have_mail_message);
  388.       dispose_words (tlist);
  389.     }
  390.  
  391.       if (find_variable ("MAIL_WARNING")
  392.       && file_access_date_changed (current_mail_file))
  393.     {
  394.       add_mail_file (current_mail_file);
  395.       printf ("The mail in %s has been read!\n", current_mail_file);
  396.     }
  397.     next_mail_file: 
  398.       free (current_mail_file);
  399.     }
  400.   free (mailpaths);
  401.   bind_variable ("_", dollar_underscore);
  402. }
  403.